<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增住户信息')" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-location-add">
            <input id="treeId" name="parentId" type="hidden" th:value="${location.locationId}"   />
            <div class="form-group">
                <label class="col-sm-3 control-label">上级地址：</label>
                <div class="col-sm-8">
                    <div class="input-group">
                        <input class="form-control" type="text" onclick="selectLocationTree()" id="treeName" readonly="true" th:value="${location.locationName}">
                        <span class="input-group-addon"><i class="fa fa-search"></i></span>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">地址名称：</label>
                <div class="col-sm-8">
                    <input id="locationName" name="locationName" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">地址类型：</label>
                <div class="col-sm-8">
                    <select name="locationType" id="locationType" class="form-control m-b" th:onchange="changeType()" th:with="type=${@dict.getType('location_type')}">
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">建筑面积：</label>
                <div class="col-sm-8">
                    <input name="buildArea" id="buildArea" class="form-control" type="number">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">计费面积：</label>
                <div class="col-sm-8">
                    <input name="costArea" id="costArea" class="form-control" type="number">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">用户姓名：</label>
                <div class="col-sm-8">
                    <input name="locationHode" id="locationHode" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">联系方式：</label>
                <div class="col-sm-8">
                    <input name="locationContact" id="locationContact" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">用户号：</label>
                <div class="col-sm-8">
                    <input name="locationNumber" id="locationNumber" class="form-control" type="text">
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var prefix = ctx + "base/location"
        $("#form-location-add").validate({
            onkeyup: false,
            rules:{
                locationName:{
                    remote: {
                        url: prefix + "/checkLocationNameUnique",
                        type: "post",
                        dataType: "json",
                        data: {
                            "parentId": function() {
                                return $("input[name='parentId']").val();
                            },
                            "locationName" : function() {
                                return $.common.trim($("#locationName").val());
                            }
                        },
                        dataFilter: function(data, type) {
                            return $.validate.unique(data);
                        }
                    }
                }
            },
            messages: {
                "locationName": {
                    remote: "地址已经存在"
                }
            },
            focusCleanup: true
        });

        $(function() {
            var locationType = $("#locationType option:selected").val();
            locationTypeVisible(locationType);
        });

        function changeType() {
            var locationType = $("#locationType option:selected").val();
            locationTypeVisible(locationType);
        }

        function locationTypeVisible(locationType) {
            if("06" == locationType){
                $("#buildArea").parents(".form-group").show();
                $("#costArea").parents(".form-group").show();
                $("#locationHode").parents(".form-group").show();
                $("#locationContact").parents(".form-group").show();
                $("#locationNumber").parents(".form-group").show();
            } else{
                $("#buildArea").parents(".form-group").hide();
                $("#costArea").parents(".form-group").hide();
                $("#locationHode").parents(".form-group").hide();
                $("#locationContact").parents(".form-group").hide();
                $("#locationNumber").parents(".form-group").hide();
            }
        }

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/add", $('#form-location-add').serialize());
            }
        }

        /*住址管理-新增-选择父住址树*/
        function selectLocationTree() {
            var options = {
                title: '住址选择',
                width: "380",
                url: prefix + "/selectLocationTree/" + $("#treeId").val(),
                callBack: doSubmit
            };
            $.modal.openOptions(options);
        }

        function doSubmit(index, layero){
            var body = layer.getChildFrame('body', index);
            $("#treeId").val(body.find('#treeId').val());
            $("#treeName").val(body.find('#treeName').val());
            layer.close(index);
        }
    </script>
</body>
</html>